package com.wwp.tbshop.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.model.bean.OrderListItemDetailBean.ProductListBean;
import com.wwp.tbshop.util.OrderListContant;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by user on 2017/6/17.
 */

public class ProductListAdapter extends BaseAdapter {
    private List<ProductListBean> mProductList;
    private Context mContext;

    public ProductListAdapter(Context context,List<ProductListBean> productList) {
        mProductList = productList;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mProductList.size();
    }

    @Override
    public Object getItem(int position) {
        return mProductList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemHolder itemHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.order_product_info, null);
            itemHolder = new ItemHolder(convertView);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ItemHolder) convertView.getTag();
        }
        itemHolder.setData(mProductList.get(position));
        return convertView;
    }

    class ItemHolder {
        @Bind(R.id.product_icon)
        ImageView mProductIcon;
        @Bind(R.id.product_name)
        TextView mProductName;
        @Bind(R.id.product_num)
        TextView mProductNum;
        @Bind(R.id.product_price)
        TextView mProductPrice;
        @Bind(R.id.product_property)
        TextView mProductProperty;

        ItemHolder(View view) {
            ButterKnife.bind(this, view);
        }

        public void setData(ProductListBean productListBean) {
            ProductListBean.ProductBean product = productListBean.product;
            List<ProductListBean.ProductBean.ProductPropertyBean> property = product.productProperty;
           mProductName.setText("商品名称"+":"+product.name);
            mProductNum.setText("商品数量"+":"+ productListBean.prodNum);
            mProductPrice.setText("商品价格"+":"+product.price);

            String product_property = "";
            for (ProductListBean.ProductBean.ProductPropertyBean propertyBean : property) {
                product_property += propertyBean.k+":"+propertyBean.v;
                product_property+=" ";
            }
            mProductProperty.setText(product_property);

            String iconUrl = OrderListContant.HOST+product.pic;
            Picasso.with(mContext).load(iconUrl).into(mProductIcon);
        }
    }
}
